<?php


namespace App\Http\Model\Job;


use Illuminate\Support\Facades\DB;

class ClassNettingModel
{

    /**
     * 班级-当日 班级&账户 轧差净额:更新表数据
     *
     * @param $data
     * @param $id
     *
     * @return int
     */
    public static function save($data, $id)
    {
        return DB::table('class_list_pay_result_history')->where('id', $id)->update($data);
    }


    /**
     * 当日待推送所有班级汇总缴费课程金额&当日待推送所有班级汇总退费课程金额
     *
     * @param $data
     * @param $createDate
     *
     * @return int[]
     */
    public static function accountSum($data, $createDate)
    {
        $list = DB::select('SELECT `pay_amt`,`refund_amt` FROM `class_list_pay_result_history` WHERE `pfs_dw_id`=:pfs_dw_id AND `create_date`=:create_date', [
            'pfs_dw_id'   => $data['pfs_dw_id'],
            'create_date' => $createDate,
        ]);
        $accountSum = ['pay_amt_sum' => 0, 'refund_amt_sum' => 0];
        if (!empty($list)) {
            foreach ($list as $val) {
                /*T-1日缴费金额 总金额*/
                $accountSum['pay_amt_sum'] = bcadd($accountSum['pay_amt_sum'], $val->pay_amt);
                /*T-1日退费金额 总金额*/
                $accountSum['refund_amt_sum'] = bcadd($accountSum['refund_amt_sum'], $val->refund_amt);
            }
        }
        return $accountSum;
    }


    /**
     * 查询需要计算的轧差日期
     *
     * @return false|string
     */
    public static function createDate($data)
    {
        if (!empty($data['create_date'])) {
            return $data['create_date'];
        }
        $data = DB::select('SELECT `create_date` FROM `class_list_pay_result_history` WHERE `pfs_dw_id`=:pfs_dw_id ORDER BY `id` DESC LIMIT 1', [
            'pfs_dw_id' => $data['pfs_dw_id'],
        ]);
        return empty($data[0]->create_date) ? date('Y-m-d') : $data[0]->create_date;
    }


    /**
     * 需要进行轧差计算的数据
     *
     * @param $data
     * @param $id
     * @param $createDate
     *
     * @return array
     */
    public static function list($data, $id, $createDate)
    {
        return DB::select('SELECT `id`,`pay_amt`,`refund_amt` FROM `class_list_pay_result_history` WHERE `id`>:id AND `pfs_dw_id`=:pfs_dw_id AND `create_date`=:create_date AND `class_id` LIKE :class_id LIMIT 1000', [
            'id'          => $id,
            'pfs_dw_id'   => $data['pfs_dw_id'],
            'create_date' => $createDate,
            'class_id'    => empty($data['class_id']) ? '%' : $data['class_id'],
        ]);
    }

}





/**
 * ClassNettingModel.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/8 21:50:00   操作:创建
 **/
